package com.gxa.community.admin.controller.content;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.community.admin.advice.Log;
import com.gxa.community.admin.service.content.HotWordService;
import com.gxa.community.core.pojo.domain.HotWord;
import com.gxa.community.core.pojo.dto.content.PageHotWordDTO;
import com.gxa.community.core.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/content")
public class HotWordController {
    @Autowired
    private HotWordService hotWordService;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 用于客户端发起远程通信获取所有热词信息，客户端不传入分页参数
     * @return
     */
    @ApiOperation("获取所有热词信息")
    @GetMapping("/remotegetallhotwords")
    public R remoteGetAllHotWords(){
        List<HotWord> list = hotWordService.list();
        return R.ok("查询成功！").put("data",list);
    }


    @ApiOperation("获取所有热词信息")
    @GetMapping("/getallhotwords")
    public R getAllHotWords(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pagaSize) {
//        if (currentPage==null && pagaSize==null){
//            //用于客户端发起远程通信获取所有热词信息，客户端不传入分页参数
//            List<HotWord> list = hotWordService.list();
//            return R.ok("查询成功！").put("data",list);
//        }
        PageHelper.startPage(currentPage, pagaSize);
        List<HotWord> list = hotWordService.list();
        if (!CollectionUtils.isEmpty(list)) {
            PageInfo<HotWord> pageInfo = new PageInfo<>(list);
            PageHotWordDTO pageHotWordDTO = new PageHotWordDTO();
            //先对当前的热词进行排序
            List<HotWord> hotWords = pageInfo.getList();
            pageHotWordDTO.setTotal(pageInfo.getTotal());
            pageHotWordDTO.setPages(pageInfo.getPages());
            pageHotWordDTO.setCurrentPage(currentPage);
            pageHotWordDTO.setPageSize(pagaSize);
            pageHotWordDTO.setHotWords(hotWords);
            return R.ok("查询成功！").put("data", pageHotWordDTO);
        } else {
            return R.error(600, "查询失败，请联系管理员！！！");
        }
    }

    @ApiOperation("根据热词名称进行搜索")
    @GetMapping("/searchhotwordsbyname")
    public R searchHotWordsByName(@RequestParam(value = "name",required = false)String name,@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pagaSize) {
        if (StringUtils.isBlank(name)){
            return this.getAllHotWords(currentPage,pagaSize);
        }else {
            PageHelper.startPage(currentPage, pagaSize);
            //包装搜索条件
            LambdaQueryWrapper<HotWord> hotWordLambdaQueryWrapper = Wrappers.lambdaQuery(HotWord.class);
            hotWordLambdaQueryWrapper.like(HotWord::getName,name);
            List<HotWord> list = hotWordService.list(hotWordLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                PageInfo<HotWord> pageInfo = new PageInfo<>(list);
                PageHotWordDTO pageHotWordDTO = new PageHotWordDTO();
                //先对当前的热词进行排序
                List<HotWord> hotWords = pageInfo.getList();
                pageHotWordDTO.setTotal(pageInfo.getTotal());
                pageHotWordDTO.setPages(pageInfo.getPages());
                pageHotWordDTO.setCurrentPage(currentPage);
                pageHotWordDTO.setPageSize(pagaSize);
                pageHotWordDTO.setHotWords(hotWords);
                return R.ok("查询成功！").put("data", pageHotWordDTO);
            } else {
                return R.error(600, "查询失败，请联系管理员！！！");
            }
        }
    }

    @ApiOperation("根据id获取单个热词信息")
    @GetMapping("/gethotwordbyid/{id}")
    public R getHotWordById(@PathVariable("id") Integer id){
        HotWord hotWord = hotWordService.getById(id);
        return hotWord!=null?R.ok("查询成功！").put("data",hotWord):R.error(600,"查询失败，请联系管理员！！");
    }

    @ApiOperation("新增热词")
    @PostMapping("/addhotword")
    @Log(title = "",action = "新增了热词")
    public R addHotWord(HotWord hotWord){
        hotWord.setReleaseTime(System.currentTimeMillis());
        HotWord one = hotWordService.getOne(new LambdaQueryWrapper<HotWord>().eq(HotWord::getName, hotWord.getName()));
        if (one != null){
            return R.error(999,"该热词已存在，请重新输入！");
        }
        boolean saveResult = hotWordService.save(hotWord);
        if (saveResult){
            //给消息队列发送消息，删除缓存的热词
            jmsMessagingTemplate.convertAndSend("DelHotWordsCache","delete");
            return R.ok("添加成功！");
        }else {
            return R.error(600,"添加失败，请联系管理员！");
        }
    }

    @ApiOperation("修改热词信息")
    @PutMapping("/updatesinglehotwordbyid")
    @Log(title = "",action = "修改了热词信息")
    public R updateSingleHotWord(HotWord hotWord){
        boolean result = hotWordService.saveOrUpdate(hotWord);
        if (result){
            //给消息队列发送消息，删除缓存的热词
            jmsMessagingTemplate.convertAndSend("DelHotWordsCache","delete");
            return R.ok("修改成功！");
        }else {
            return R.error(600,"修改失败，请联系管理员！！");
        }
    }

    @ApiOperation("根据id删除热词信息")
    @DeleteMapping("/deletehotwordbyid/{id}")
    @Log(title = "",action = "删除了一个热词")
    public R delHotWordById(@PathVariable("id") Integer id){
        boolean result = hotWordService.removeById(id);

        if (result){
            //给消息队列发送消息，删除缓存的热词
            jmsMessagingTemplate.convertAndSend("DelHotWordsCache","delete");
            return R.ok("删除成功！");
        }else {
            return R.error(600,"删除失败，请联系管理员！！");
        }
    }

    @ApiOperation("上线或下线热词")
    @PutMapping("/updatehotwordstatus/{id}")
    @Log(title = "",action = "上线或下线热词")
    public R updateHotWordStatus(@PathVariable("id") Integer id){
        HotWord hotWord = hotWordService.getById(id);
        if (hotWord.getStatus()==1){
            hotWord.setStatus(2);
            boolean result = hotWordService.saveOrUpdate(hotWord);
            if (result){
                //给消息队列发送消息，删除缓存的热词
                jmsMessagingTemplate.convertAndSend("DelHotWordsCache","delete");
                return R.ok("修改成功！");
            }else {
                return R.error(600,"修改失败，请联系管理员！！");
            }
        }else{
            hotWord.setStatus(1);
            boolean result = hotWordService.saveOrUpdate(hotWord);
            if (result){
                //给消息队列发送消息，删除缓存的热词
                jmsMessagingTemplate.convertAndSend("DelHotWordsCache","delete");
                return R.ok("修改成功！");
            }else {
                return R.error(600,"修改失败，请联系管理员！！");
            }
        }
    }
}